<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_计算属性案例</title>
    <style>
        #app {
            width: 500px;
            margin: 0 auto;
        }
    </style>

</head>
<body>
    <div id="app">
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>名称</th>
                    <th>数量</th>
                </tr>
            </thead>

            <tbody>
                <tr v-for="book in books">
                    <td >{{book.id}}</td>
                    <td >{{book.name}}</td>
                    <td >{{book.number}}</td>
                </tr>
            </tbody>
        </table>
        <h3>书籍总数:{{totalNumber}}</h3>
    </div>
</body>
<script src="../vue.js"></script>
<script>
    new Vue({
        el: '#app',
        data: {
            books: [
                {"id":1,"name":"西游记","number":100},
                {"id":2,"name":"水浒传","number":400},
                {"id":3,"name":"红楼梦","number":200},
                {"id":4,"name":"三国演义","number":399},
            ]
        },
        computed: {
            totalNumber(){
                let result = 0;
                for (let i in this.books) {
                    result += this.books[i].number
                }
                return result
            }
        }
    })
</script>
</html>